package cn.iocoder.yudao.module.promotion.coupon.controller.admin;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.promotion.coupon.convert.CouponRecordConvert;
import cn.iocoder.yudao.module.promotion.coupon.dal.dataobject.CouponRecordDO;
import cn.iocoder.yudao.module.promotion.coupon.service.CouponRecordService;
import cn.iocoder.yudao.module.promotion.coupon.vo.CouponRecordPageItemRespVO;
import cn.iocoder.yudao.module.promotion.coupon.vo.CouponRecordPageReqVO;
import cn.iocoder.yudao.module.promotion.coupon.vo.CouponSendReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "管理后台 - 优惠劵领取记录")
@RestController
@RequestMapping({"/promotion/coupon", "/promotion/coupon-record"})
@Validated
public class CouponRecordController {

    @Resource
    private CouponRecordService couponRecordService;
    @Resource
    private MemberUserApi memberUserApi;

    @DeleteMapping("/delete")
    @Operation(summary = "回收优惠劵")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasRole('shop_admin')")
    public CommonResult<Boolean> deleteCoupon(@RequestParam("id") Long id) {
        couponRecordService.deleteCoupon(id);
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得优惠劵分页")
    @PreAuthorize("@ss.hasPermission('promotion:coupon-record:query')")
    public CommonResult<PageResult<CouponRecordPageItemRespVO>> getCouponPage(@Valid CouponRecordPageReqVO pageVO) {
        PageResult<CouponRecordDO> pageResult = couponRecordService.getCouponPage(pageVO);
        PageResult<CouponRecordPageItemRespVO> pageResulVO = CouponRecordConvert.INSTANCE.convertPage(pageResult);
        if (CollUtil.isEmpty(pageResulVO.getList())) {
            return success(pageResulVO);
        }

        // 读取用户信息，进行拼接
        Map<Long, UserSimpleVO> userMap = memberUserApi.getUserSimpleMap(convertSet(pageResult.getList(), CouponRecordDO::getUserId));
        pageResulVO.getList().forEach(itemRespVO -> MapUtils.findAndThen(userMap, itemRespVO.getUserId(),
                userRespDTO -> itemRespVO.setNickname(userRespDTO.getNickname())));
        return success(pageResulVO);
    }

    @PostMapping("/send")
    @Operation(summary = "发送优惠劵")
    @PreAuthorize("@ss.hasAnyRoles('super_admin','platform_admin','tenant_admin','shop_admin')")
    public CommonResult<Boolean> sendCoupon(@Valid @RequestBody CouponSendReqVO reqVO) {
        couponRecordService.takeCouponByAdmin(reqVO.getTemplateId(), reqVO.getUserIds());
        return success(true);
    }

}
